//
//  SASalesOrderStatementErrorView.m
//  Saas
//
//  Created by bing on 2020/6/12.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SASalesOrderStatementErrorView.h"
#import "SASalesOrderStatementErrorModel.h"
#import "SASalesOrderStatementErrorCell.h"

@interface SASalesOrderStatementErrorView()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray  *dataArray;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *backGroundView;

@end

@implementation SASalesOrderStatementErrorView

+(instancetype)shareCornView
{
    SASalesOrderStatementErrorView * shareView = [[SASalesOrderStatementErrorView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.3);
        [self creatUI];
    }
    
    return self;
    
}

-(void)creatUI
{
    

    
    UIImageView *groundImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - kScaleNum(300))/2, (kScreenHeight - kScaleNum(250))/2, kScaleNum(300), kScaleNum(250))];
    //groundImageView.backgroundColor = [UIColor cyanColor];
    groundImageView.image = [UIImage imageNamed:@"sales_order_statement_error_image"];
    groundImageView.userInteractionEnabled = YES;
    [self addSubview:groundImageView];
    
    UILabel *lowerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(15), kScaleNum(15), kScaleNum(270), kScaleNum(65))];
    lowerLabel.textAlignment = NSTextAlignmentCenter;
    lowerLabel.numberOfLines = 3;
    lowerLabel.text = @"您选择的以下商品信息已经发生改变，您可选择立即全部清除并重新添加，或手动清除。";
    lowerLabel.textColor = kColor(51, 51, 51, 1);
    lowerLabel.font = kFont(15);
    [groundImageView addSubview:lowerLabel];
    
      UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      deleteBtn.frame = SARectMake(0, 205, 150, 45);
    [deleteBtn setTitle:@"取消" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:kLightgGrayColor forState:UIControlStateNormal];
      [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
      [groundImageView addSubview:deleteBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = SARectMake(150, 205, 150, 45);
   // confirmBtn.backgroundColor = [UIColor redColor];
    [confirmBtn setTitle:@"全部清除" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmBtnpEvent) forControlEvents:UIControlEventTouchUpInside];
    [groundImageView addSubview:confirmBtn];

    [groundImageView addSubview:self.tableView];
  
}



-(void)confirmBtnpEvent
{
    if (self.removeBlock) {
        self.removeBlock(self.dataArray);
    }
    [UIView animateWithDuration:0.5f animations:^{
          
          self.alpha = 0.0;
      } completion:^(BOOL finished) {
          [self removeFromSuperview];
      }];
}

-(void)loadData:(NSArray *)array
{

    for (SASalesOrderStatementErrorModel *model in array) {
        for (SASalesOrderStatementErrorSkuList *list in model.skuList) {
            list.productCode = model.productCode;
            list.productName = model.productName;
            [self.dataArray addObject:list];
        }
    }
    [self.tableView reloadData];
}


- (void)showView:(UIViewController *)Sview
{
    if(Sview==nil){
        
        [[[UIApplication sharedApplication].windows lastObject] addSubview:self];
        
    }else{
        //[view addSubview:self];
        [Sview.view addSubview:self];
    }
}

-(void)shareDissTapEvent
{

    [UIView animateWithDuration:0.5f animations:^{
          
          self.alpha = 0.0;
      } completion:^(BOOL finished) {
          [self removeFromSuperview];
      }];
}

#pragma mark--tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScaleNum(19);
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SASalesOrderStatementErrorSkuList *model = self.dataArray[indexPath.row];
    SASalesOrderStatementErrorCell *cell = [SASalesOrderStatementErrorCell cellForTableView:tableView];
    [cell loadData:model];
    return cell;
}


#pragma mark - lazy load

-(NSMutableArray *)dataArray
{
    if(_dataArray == nil)
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView
{
    if(_tableView == nil)
    {
        _tableView = [[UITableView alloc]initWithFrame:SARectMake(0, 85, 300, 115) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
    return _tableView;
}

@end
